package com.abc.pay.client;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.StringTokenizer;

@Slf4j
public class MerchantParaFromFile extends MerchantParaFactory {

    private static MerchantPara paraWeb = null;

    private static boolean iIsInitialed = false;

    private TrustMerchant trustMerchant = new TrustMerchant();

    private String configSourceMethod;

    private String configSourceClass;

    private String trustPayConnectMethod;

    private String trustPayServerName;

    private String trustPayServerPort;

    private String trustPayNewLine;

    private String trustPayTrxURL;

    private String trustPayFileTrxURL;

    private String trustPayIETrxURL;

    private String merchantErrorURL;

    private String trustPayCertFile;

    private String merchantID;

    private String printLog;

    private String logPath;

    private String merchantCertFile;

    private String merchantCertPassword;

    private String pfxFile;

    private String pfxPwd;

    private String cerFile;

    private String appId;

    private String appSecret;

    public MerchantParaFromFile() {
        if(StringUtils.isEmpty(configSourceMethod)){
            configSourceMethod = trustMerchant.getConfigSourceMethod();
        }
        if(StringUtils.isEmpty(configSourceClass)){
            configSourceClass = trustMerchant.getConfigSourceClass();
        }
        if(StringUtils.isEmpty(trustPayConnectMethod)){
            trustPayConnectMethod = trustMerchant.getTrustPayConnectMethod();
        }
        if(StringUtils.isEmpty(trustPayServerName)){
            trustPayServerName = trustMerchant.getTrustPayServerName();
        }
        if(StringUtils.isEmpty(trustPayServerPort)){
            trustPayServerPort = trustMerchant.getTrustPayServerPort();
        }
        if(StringUtils.isEmpty(trustPayNewLine)){
            trustPayNewLine = trustMerchant.getTrustPayNewLine();
        }
        if(StringUtils.isEmpty(trustPayTrxURL)){
            trustPayTrxURL = trustMerchant.getTrustPayTrxURL();
        }
        if(StringUtils.isEmpty(trustPayFileTrxURL)){
            trustPayFileTrxURL = trustMerchant.getTrustPayFileTrxURL();
        }
        if(StringUtils.isEmpty(trustPayIETrxURL)){
            trustPayIETrxURL = trustMerchant.getTrustPayIETrxURL();
        }
        if(StringUtils.isEmpty(merchantErrorURL)){
            merchantErrorURL = trustMerchant.getMerchantErrorURL();
        }
        if(StringUtils.isEmpty(trustPayCertFile)){
            trustPayCertFile = trustMerchant.getTrustPayCertFile();
        }
        if(StringUtils.isEmpty(merchantID)){
            merchantID = trustMerchant.getMerchantID();
        }
        if(StringUtils.isEmpty(printLog)){
            printLog = trustMerchant.getPrintLog();
        }
        if(StringUtils.isEmpty(logPath)){
            logPath = trustMerchant.getLogPath();
        }
        if(StringUtils.isEmpty(merchantCertFile)){
            merchantCertFile = trustMerchant.getMerchantCertFile();
        }
        if(StringUtils.isEmpty(merchantCertPassword)){
            merchantCertPassword = trustMerchant.getMerchantCertPassword();
        }
        if(StringUtils.isEmpty(pfxFile)){
            pfxFile = trustMerchant.getPfxFile();
        }
        if(StringUtils.isEmpty(pfxPwd)){
            pfxPwd = trustMerchant.getPfxPwd();
        }
        if(StringUtils.isEmpty(cerFile)){
            cerFile = trustMerchant.getCerFile();
        }
        if(StringUtils.isEmpty(appId)){
            appId = trustMerchant.getAppId();
        }
        if(StringUtils.isEmpty(appSecret)){
            appSecret = trustMerchant.getAppSecret();
        }
    }

    public void refreshConfig() {
        iIsInitialed = false;
    }

    public MerchantPara getMerchantPara() throws TrxException {
        if (!iIsInitialed) {
            try {
                paraWeb = MerchantParaWeb.getUniqueInstance();
            } catch (TrxException e) {
                e.printStackTrace();
                log.error(Util.getExceptionString(e));
            }
            this.init(paraWeb);
            iIsInitialed = true;
        }
        return paraWeb;
    }

    public void init(MerchantPara para) throws TrxException {
        log.info("[Trustpay商户端API] - 初始 - 开始====================");
        log.info("[Trustpay商户端API] - 初始 - 读取系统配置文件");
        String iLogPath = "";
        boolean iIsPrintLog = Boolean.parseBoolean(printLog);
        if (iIsPrintLog) {
            iLogPath = logPath;
        }
        para.setTrustPayConnectMethod(trustPayConnectMethod);
        para.setTrustPayServerName(trustPayServerName);
        para.setTrustPayServerPort(trustPayServerPort);
        para.setTrustPayTrxURL(trustPayTrxURL);
        para.setTrustPayFileTrxURL(trustPayFileTrxURL);
        para.setTrustPayTrxIEURL(trustPayIETrxURL);
        try {
            para.setTrustPayCertFile(MerchantPara.getCertificate(trustPayCertFile).getEncoded());
        } catch (TrxException e) {
            log.error(Util.getExceptionString(e));
            throw new TrxException(e.getCode(), e.getMessage());
        } catch (Exception ex) {
            log.error(Util.getExceptionString(ex));
        }
        para.setTrustPayCertFileName(trustPayCertFile);
        para.setPrintLog(iIsPrintLog);
        para.setLogPath(iLogPath);

        if (trustPayConnectMethod.length() == 0) {
            throw new TrxException("1001", "商户端配置文件中参数设置错误 - 线上支付平台通讯方式[TrustPayConnectMethod]配置错误！");
        } else {
            log.info("[Trustpay商户端API] - 初始 - 线上支付平台通讯方式 = [" + trustPayConnectMethod + "]");
            if (trustPayServerName.length() == 0) {
                throw new TrxException("1001", "商户端配置文件中参数设置错误 - 线上支付平台服务器IP[TrustPayServerName]配置错误！");
            } else {
                log.info("[Trustpay商户端API] - 初始 - 线上支付平台服务器IP = [" + trustPayServerName + "]");
                if (trustPayServerPort.length() == 0) {
                    throw new TrxException("1001", "商户端配置文件中参数设置错误 - 线上支付平台交易端口[TrustPayServerPort]配置错误！");
                } else {
                    log.info("[Trustpay商户端API] - 初始 - 线上支付平台交易端口 = [" + trustPayServerPort + "]");
                    if (trustPayTrxURL.length() == 0 && trustPayIETrxURL.length() == 0) {
                        throw new TrxException("1001", "商户端配置文件中参数设置错误 - 线上支付平台交易网址[TrustPayTrxURL]配置错误！");
                    } else {
                        log.info("[Trustpay商户端API] - 初始 - 线上支付平台交易网址 = [" + trustPayTrxURL + "]");
                        log.info("[Trustpay商户端API] - 初始 - 线上支付平台对账单下载网址 = [" + trustPayFileTrxURL + "]");
                        log.info("[Trustpay商户端API] - 初始 - 商户通过浏览器提交线上支付平台交易网址 = [" + trustPayIETrxURL + "]");
                        log.info("[Trustpay商户端API] - 初始 - 商户通过浏览器提交线上支付平台交易网址 = [" + merchantErrorURL + "]");
                        if (trustPayCertFile.length() == 0) {
                            throw new TrxException("1001", "商户端配置文件中参数设置错误 - 线上支付平台证书[tTrustPayCertFile]配置错误！");
                        } else {
                            log.info("[Trustpay商户端API] - 初始 - 线上支付平台证书 = [" + trustPayCertFile + "]");
                            if (iIsPrintLog && iLogPath.length() == 0) {
                                throw new TrxException("1001", "商户端配置文件中参数设置错误 - 商户日志目录[LogPath]配置错误！");
                            } else {
                                log.info("[Trustpay商户端API] - 初始 - 日志文件目录 = [" + iLogPath + "]");
                                ArrayList<String> iMerchantIDList = new ArrayList();
                                if (merchantID.length() == 0) {
                                    throw new TrxException("1001", "商户端配置文件中参数设置错误 - 商户号[MerchantID]配置错误！");
                                } else {
                                    StringTokenizer st = new StringTokenizer(merchantID, ",");
                                    while(st.hasMoreTokens()) {
                                        String merchantID = st.nextToken();
                                        iMerchantIDList.add(merchantID);
                                    }
                                    para.setMerchantIDList(iMerchantIDList);
                                    log.info("[Trustpay商户端API] - 初始 - 商户编号 = [" + merchantID + "]");
                                    StringTokenizer stCertFile = new StringTokenizer(merchantCertFile, ",");
                                    StringTokenizer stCertPassword = new StringTokenizer(merchantCertPassword, ",");
                                    if (stCertFile.countTokens() != stCertPassword.countTokens()) {
                                        throw new TrxException("1001", "商户端配置文件中参数设置错误", "商户证书储存目录档名[MerchantCertFile]或商户私钥加密密码[MerchantCertPassword]配置错误！");
                                    } else {
                                        ArrayList<String> tCertFileList = new ArrayList();
                                        ArrayList<String> tCertFileListTemp = new ArrayList();
                                        ArrayList<String> tCertPasswordList = new ArrayList();
                                        while(stCertFile.hasMoreTokens()) {
                                            tCertFileListTemp.add(stCertFile.nextToken());
                                        }
                                        para.setMerchantCertFileListString(tCertFileListTemp);
                                        while(stCertPassword.hasMoreTokens()) {
                                            tCertPasswordList.add(stCertPassword.nextToken());
                                        }
                                        para.setMerchantCertPasswordList(tCertPasswordList);
                                        CertHelper.bindMerchantCertificateByFile(para, tCertFileListTemp, tCertPasswordList);
                                        log.info("[Trustpay商户端API] - 初始 - 商户证书及私钥初始完成");
                                        log.info("[Trustpay商户端API] - 初始 - 完成====================");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

}

